﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Infrastructure.WpfClient.Accessibility
{
    public class AccesibilitySQLClientProvider  : Microsoft.Synchronization.Data.ClientSyncProvider
    {

        public Int64 AnchorToUInt64(byte[] last_received_anchor )
        {
            var memStream  = new MemoryStream(last_received_anchor);
            var formatter = new BinaryFormatter();
            return (Int64)formatter.Deserialize(memStream);
        }
        private Int64? LastSync = null;


        public override Microsoft.Synchronization.Data.SyncContext ApplyChanges(Microsoft.Synchronization.Data.SyncGroupMetadata groupMetadata, System.Data.DataSet dataSet, Microsoft.Synchronization.Data.SyncSession syncSession)
        {




            var ints = AnchorToUInt64(groupMetadata.NewAnchor.Anchor);
            this.LastSync = ints;

            var ct = new Microsoft.Synchronization.Data.SyncContext();                        
            ct.GroupProgress = new Microsoft.Synchronization.Data.SyncGroupProgress(groupMetadata, dataSet);
            ct.GroupProgress.TablesProgress.Add( new Microsoft.Synchronization.Data.SyncTableProgress(){ TableName = groupMetadata.TablesMetadata[0].TableName,  ChangesApplied= dataSet.Tables[0].Rows.Count  } );            
            return ct;
        }

        public override void BeginTransaction(Microsoft.Synchronization.Data.SyncSession syncSession)
        {
            
        }

        private Guid _Id = Guid.NewGuid(); 
        public override Guid ClientId
        {
            get
            {
                
                return _Id;
            }
            set
            {
                _Id = value;
            }
        }

     
        public override void Dispose()
        {
            
        }

        public override void EndTransaction(bool commit, Microsoft.Synchronization.Data.SyncSession syncSession)
        {
            
        }        

        public override Microsoft.Synchronization.Data.SyncAnchor GetTableReceivedAnchor(string tableName)
        {


            if (this.LastSync != null)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                var memo = new MemoryStream();
                formatter.Serialize(memo, this.LastSync);
                var anchor = new Microsoft.Synchronization.Data.SyncAnchor(memo.ToArray());
                return anchor;
            }
            else
            {
                var anchor = new Microsoft.Synchronization.Data.SyncAnchor();
                return anchor;
            }           
            
        }

        public override Microsoft.Synchronization.Data.SyncAnchor GetTableSentAnchor(string tableName)
        {  
            var anchor = new Microsoft.Synchronization.Data.SyncAnchor();
            return anchor;
        }

        public override void SetTableReceivedAnchor(string tableName, Microsoft.Synchronization.Data.SyncAnchor anchor)
        {
            
        }

        public override void SetTableSentAnchor(string tableName, Microsoft.Synchronization.Data.SyncAnchor anchor)
        {
            
        }



        public override void CreateSchema(Microsoft.Synchronization.Data.SyncTable syncTable, Microsoft.Synchronization.Data.SyncSchema syncSchema)
        {
            throw new NotImplementedException();
        }

        public override Microsoft.Synchronization.Data.SyncContext GetChanges(Microsoft.Synchronization.Data.SyncGroupMetadata groupMetadata, Microsoft.Synchronization.Data.SyncSession syncSession)
        {
            throw new NotImplementedException();
        }
    }
}
